package com.af.community.smart.business

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.utils.toFormatString
import com.af.community.smart.viewmodel.HandleViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HandleListActivity : BaseActivity() {

    private lateinit var mTitle: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        mTitle = intent.getStringExtra("title").orEmpty()
        initTitle(title = mTitle)

        val typeId = intent.getStringExtra("id").orEmpty()
        if (typeId.isNotEmpty()) {
            getViewModel().getGuideDetail(typeId)
                .bindLifeCycle(getLifecycleOwner())
                .subscribeByFinally(
                    event = getViewModel(),
                    onSuccess = {
                        mList.addItems(it)
                        mAdapter.updateData(mList)
                    },
                    onFinally = {
                        empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                    }
                )
        }
    }

    override fun getViewModel() = getViewModel<HandleViewModel>()

    @SuppressLint("SetTextI18n")
    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        empty_hint.text = "暂无相关${mTitle}信息！"
        swipe_refresh.isEnabled = false
        recycle_list.loadLinear(baseContext)

        val type = intent.getStringExtra("type").orEmpty()
        mAdapter = SlimAdapter.create()
            .apply {
                when (type) {
                    "0" ->
                        register<CommonData>(R.layout.item_handle_list01) { data, injector ->
                            injector.text(R.id.item_handle_title, data.title)
                                .text(
                                    R.id.item_handle_time,
                                    data.createDate?.toFormatString() ?: ""
                                )
                                .clicked(R.id.item_handle) {
                                    startActivity<WebActivity>(
                                        "type" to "handleList",
                                        "title" to "详情",
                                        "content" to data.content.orEmpty()
                                    )
                                }
                        }
                    "4" ->
                        register<CommonData>(R.layout.item_handle_list02) { data, injector ->
                            injector.text(R.id.item_handle_title, data.title)
                                .text(R.id.item_handle_addr, data.address)
                                .text(R.id.item_handle_tel, "电话：${data.tel.orEmpty()}")
                                .clicked(R.id.item_handle) {
                                    if (data.tel.isNullOrEmpty()) return@clicked
                                    else makeCall(data.tel.orEmpty())
                                }
                        }
                }
            }
            .attachTo(recycle_list)
    }

}
